﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CalcEngine;

namespace TestProject1
{
    /// <summary>
    /// Summary description for CalculationEngineTest
    /// </summary>
    [TestClass]
    public class BatchCalculationTest
    {
        public BatchCalculationTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void BatchCalculationTest1()
        {
            using (CalculationContext context = new CalculationContext())
            {
                BatchCalculationEngine calc = new BatchCalculationEngine(context);
                Assert.AreEqual(0,calc.Results.Count);

                calc.Expressions.Add("1+1=");
                calc.Expressions.Add("1+3=");
                calc.Expressions.Add("sbrt(1.2*5*6)/5=");
                calc.Run();
                Assert.AreEqual(3, calc.Results.Count);
                Assert.AreEqual(2, calc.Results[0]);
                Assert.AreEqual(4, calc.Results[1]);
                Assert.AreEqual(1.2, calc.Results[2]);
            }
        }
        [TestMethod]
        public void BatchCalculationTestWithEvent1()
        {
            using (CalculationContext context = new CalculationContext())
            {
                BatchCalculationEngine calc = new BatchCalculationEngine(context);
                Assert.AreEqual(0, calc.Results.Count);

                int counter1 = 0;
                calc.BeforeCalculation += (sender,e) =>
                    {
                        Assert.IsTrue(sender is BatchCalculationEngine);

                        Assert.IsTrue(e.Index==counter1);
                        counter1++;
                    };
                int counter2 = 0;
                calc.CalculationComplete += (sender, e) =>
                    {
                        Assert.IsTrue(counter1 > counter2);
                        Assert.IsTrue(e.Index == counter2);
                        counter2++;
                    };
                calc.Expressions.Add("1+1=");
                calc.Expressions.Add("1+3=");
                calc.Expressions.Add("sbrt(1.2*5*6)/5=");
                calc.Run();
                Assert.AreEqual(3, counter1);
                Assert.AreEqual(3, counter2);
            }
        }
        [TestMethod]
        public void BatchCalculationTestWithEvent2()
        {
            using (CalculationContext context = new CalculationContext())
            {
                BatchCalculationEngine calc = new BatchCalculationEngine(context);
                Assert.AreEqual(0, calc.Results.Count);

                calc.BeforeCalculation += (sender, e) =>
                {
                    if (e.Index == 0)
                    {
                        Assert.AreEqual("1+1=", e.Expression);
                    }
                    else if (e.Index == 1)
                    {
                        Assert.AreEqual("1+3=", e.Expression);
                    }
                    else if (e.Index == 2)
                    {
                         Assert.AreEqual("sbrt(1.2*5*6)/5=", e.Expression);
                    }
                };
                calc.CalculationComplete += (sender, e) =>
                {
                    if (e.Index == 0)
                    {
                        Assert.AreEqual(2, e.Result);
                    }
                    else if (e.Index == 1)
                    {
                        Assert.AreEqual(4, e.Result);
                    }
                    else if (e.Index == 2)
                    {
                        Assert.AreEqual(1.2, e.Result);
                    }
                };
                calc.Expressions.Add("1+1=");
                calc.Expressions.Add("1+3=");
                calc.Expressions.Add("sbrt(1.2*5*6)/5=");
                calc.Run();

            }
        }
    }
}
